package com.deyou.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.deyou.Constants;
import com.deyou.DeYouApplication;
import com.deyou.R;
import com.deyou.activity.LoginActivity;
import com.deyou.model.CollectionBean;
import com.deyou.model.HouseResourceAttrBean;
import com.deyou.util.AndroidUtils;
import com.deyou.util.GlideImageLoader;
import com.deyou.util.SPUtils;
import com.deyou.util.Urls;
import com.deyou.widget.CircleImageView;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yangyuancheng on 2017/5/12.
 */

public class HouseSearchResultAdapter extends BaseQuickAdapter<HouseResourceAttrBean, HouseSearchResultAdapter.ViewHolder> {
    private Context mContext;
    private OnCollectItemClickListener collectListener;

    public HouseSearchResultAdapter(Context context) {
        super(R.layout.item_collection);
        this.mContext = context;
    }

    @Override
    protected void convert(ViewHolder holder, final HouseResourceAttrBean item) {
        holder.starLayout.removeAllViews();
        for (int i = 0; i < 5; i++) {
            ImageView ivStar = new ImageView(mContext);
            if (i < item.evaluateStar) {
                ivStar.setImageResource(R.drawable.ic_star_sel);
            } else {
                ivStar.setImageResource(R.drawable.ic_star_nor);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(44, 40);
            ivStar.setLayoutParams(params);
            ivStar.setPadding(5, 0, 5, 0);
            holder.starLayout.addView(ivStar);
        }

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(DeYouApplication.mWidth - 64, (DeYouApplication.mWidth - 64) * 400 / 662);
        params.setMargins(32, 0, 32, 0);
        holder.bgCollection.setLayoutParams(params);

        holder.tvPrice.setText(Constants.RMB_UNIT + item.resourcePrice);
        holder.tvIntro.setText(item.resourceName);
        holder.tvHomestayAttr.setText(item.resourceTypeName + "-" + item.resourceCityName);
        holder.tvComments.setText(item.evaluateNum + "条评论");
        new GlideImageLoader().displayImage(mContext, Urls.HouseResourceImageBaseUrl + item.resourceMainPic, holder.bgCollection);
//        new GlideImageLoader().displayImage(mContext, Urls.UserImageBaseUrl + item.userPic, holder.imgPhoto);
        holder.imgPhoto.setVisibility(View.GONE);
        holder.ivCollectLabel.setVisibility(View.VISIBLE);
        if (1 == item.collection)
            holder.ivCollectLabel.setImageResource(R.drawable.ic_collected);
        else
            holder.ivCollectLabel.setImageResource(R.drawable.ic_uncollect);

        holder.ivCollectLabel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(SPUtils.getStringByKey(mContext, Constants.UserId))) {
                    collectListener.collectItemClick(item);
                } else {
                    AndroidUtils.startNextActivity((Activity) mContext, LoginActivity.class, null, false);
                }
            }
        });
    }

    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.bg_collection)
        ImageView bgCollection;
        @BindView(R.id.iv_collect_label)
        ImageView ivCollectLabel;
        @BindView(R.id.img_photo)
        CircleImageView imgPhoto;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_intro)
        TextView tvIntro;
        @BindView(R.id.tv_homestay_attr)
        TextView tvHomestayAttr;
        @BindView(R.id.star_layout)
        LinearLayout starLayout;
        @BindView(R.id.tv_comments)
        TextView tvComments;
        @BindView(R.id.item_root_view)
        LinearLayout itemRootView;

        ViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnCollectItemClickListener {
        void collectItemClick(HouseResourceAttrBean item);
    }

    public void setCollectItemClick(OnCollectItemClickListener collectListener) {
        this.collectListener = collectListener;
    }
}
